package jinyilw.tools.net.dev;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public final class NetworkMessage extends NetworkPacket
{

	public static final short ID = -255; // the id for network message

	private String message;

	public NetworkMessage()
	{
	}

	public NetworkMessage(String message)
	{
		this.message = message;
	}

	@Override
	public short getID()
	{
		return NetworkMessage.ID;
	}

	@Override
	public void setId(short id)
	{
		throw new UnsupportedOperationException(
				"To change NetworkMessage ID, use NetworkMessage.ID = new_id instead. "
						+ "Or use NetworkObject to send String message with unique ID.");
	}

	public String getMessage()
	{
		return this.message;
	}

	public void setMessage(String message)
	{
		this.message = message;
	}

	@Override
	public void read(DataInputStream input) throws IOException
	{
		this.message = input.readUTF();
	}

	@Override
	public void write(DataOutputStream output) throws IOException
	{
		output.writeUTF(this.message);
	}

	@Override
	public String toString()
	{
		if (!NetworkConfig.DEBUG)
		{
			return super.toString();
		}
		return "NetworkMessage: " + this.message;
	}
}
